#!/bin/bash
# set -xeuo pipefail

# --------------------------
# User Configuration (Edit these!)
# --------------------------
PKG_NAME="rmenu"
PKG_VERSION="0.99"
PKG_ARCH="amd64"
MAINTAINER="etcix<1690544550@qq.com>"
PKG_DESCRIPTION="Simple pipe-filter menu launcher with Raylib GUI
 A lightweight menu launcher similar to dmenu, with real-time input filtering
 and immediate command execution. Uses local Raylib library for rendering."

# --------------------------
# Fixed Paths
# --------------------------
SRC_DIR="$(pwd)"
BUILD_DIR="${SRC_DIR}/rmenu_deb_build"  # Correct variable (all uppercase)
DEB_DEST="${SRC_DIR}/${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}.deb"
BIN_DEST="${BUILD_DIR}/usr/local/bin"


# --------------------------
# Step 1: Check Dependencies
# --------------------------
echo "=== Checking build dependencies ==="
if ! command -v gcc &> /dev/null; then
    echo "ERROR: gcc is not installed. Install with: sudo apt install gcc"
    exit 1
fi
if ! command -v dpkg-deb &> /dev/null; then
    echo "ERROR: dpkg-deb is not installed. Install with: sudo apt install dpkg-dev"
    exit 1
fi
if [ ! -f "${SRC_DIR}/lib/libraylib.a" ]; then
    echo "ERROR: Local Raylib library not found at ${SRC_DIR}/lib/libraylib.a"
    exit 1
fi
if [ ! -d "${SRC_DIR}/include/" ]; then
    echo "ERROR: Raylib include directory not found at ${SRC_DIR}/include/"
    exit 1
fi
for file in rmenu.c stest.c rmenu_run rmenu_path; do
    if [ ! -f "${SRC_DIR}/${file}" ]; then
        echo "ERROR: Source file missing: ${SRC_DIR}/${file}"
        exit 1
    fi
done


# --------------------------
# Step 2: Clean Previous Build (TYPO FIXED HERE)
# --------------------------
echo -e "\n=== Cleaning previous build ==="
rm -rf "${BUILD_DIR}" "${DEB_DEST}"  # Fixed: BUILD_DIR (not BUILD_dir)


# --------------------------
# Step 3: Create Debian Package Structure
# --------------------------
echo -e "\n=== Creating DEB directory structure ==="
mkdir -p "${BIN_DEST}"
mkdir -p "${BUILD_DIR}/DEBIAN"

cat > "${BUILD_DIR}/DEBIAN/control" << EOF
Package: ${PKG_NAME}
Version: ${PKG_VERSION}
Section: utils
Priority: optional
Architecture: ${PKG_ARCH}
Maintainer: ${MAINTAINER}
Depends: libc6 (>= 2.28)
Description: ${PKG_DESCRIPTION}
EOF

chmod 755 "${BUILD_DIR}/DEBIAN"


# --------------------------
# Step 4: Compile Binaries
# --------------------------
echo -e "\n=== Compiling rmenu (with local Raylib) ==="
gcc -o "${BIN_DEST}/rmenu" \
    "${SRC_DIR}/rmenu.c" \
    -I "${SRC_DIR}/include/" \
    -L "${SRC_DIR}/lib/" \
    -l:libraylib.a \
    -lm \
    -lpthread \
    -ldl \
    -lrt \
    -lX11

echo -e "\n=== Compiling stest ==="
gcc -o "${BIN_DEST}/stest" \
    "${SRC_DIR}/stest.c" \
    -O2


# --------------------------
# Step 5: Copy Additional Binaries
# --------------------------
echo -e "\n=== Copying rmenu_run and rmenu_path ==="
cp "${SRC_DIR}/rmenu_run" "${BIN_DEST}/"
chmod 755 "${BIN_DEST}/rmenu_run"

cp "${SRC_DIR}/rmenu_path" "${BIN_DEST}/"
chmod 755 "${BIN_DEST}/rmenu_path"


# --------------------------
# Step 6: Verify Binaries
# --------------------------
echo -e "\n=== Verifying compiled binaries ==="
for bin in rmenu stest rmenu_run rmenu_path; do
    if [ ! -x "${BIN_DEST}/${bin}" ]; then
        echo "ERROR: Binary ${bin} is missing or not executable!"
        exit 1
    fi
    echo "✓ ${bin} is valid and executable"
done


# --------------------------
# Step 7: Build Debian Package
# --------------------------
echo -e "\n=== Building DEB package ==="
dpkg-deb --build --root-owner-group "${BUILD_DIR}" "${DEB_DEST}"


# --------------------------
# Step 8: Cleanup & Final Message
# --------------------------
echo -e "\n=== Cleaning up temporary files ==="
rm -rf "${BUILD_DIR}"

echo -e "\n=== Build Complete! ==="
echo "Debian package created at: ${DEB_DEST}"
echo "To install: sudo dpkg -i ${DEB_DEST}"
echo "To fix missing dependencies (if any): sudo apt -f install"
